package org.streets.eis.component.combobox;

import java.util.Iterator;
import java.util.List;

import org.apache.wicket.Response;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AutoCompleteSettings;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.model.IModel;

import org.streets.eis.component.WebResources;
import org.streets.eis.component.model.ListItem;

/**
 *  注意：html form的reset动作对此控件无效
 *
 */
public abstract class ListComboBox extends ComboBox<List<ListItem>>{
	private static final long serialVersionUID = 1L;
	
	public ListComboBox(String id, IModel<ListItem> model,List<ListItem> dataModel){
		this(id,model,dataModel,true);
	}
	
	public ListComboBox(String id, IModel<ListItem> model,List<ListItem> dataModel,boolean canInput){
		super(id,model,dataModel,canInput);
	}
	
	public ListComboBox(String id, IModel<ListItem> model,List<ListItem> dataModel,boolean canInput, AutoCompleteSettings settings){
		super(id, model,dataModel,canInput,settings);
	}
	
	public ListComboBox(String id,List<ListItem> dataModel){
		this(id,dataModel,true);
	}
	
	public ListComboBox(String id,List<ListItem> dataModel,boolean canInput){
		super(id,dataModel,canInput);
	}
	
	public ListComboBox(String id,List<ListItem> dataModel,boolean canInput, AutoCompleteSettings settings){
		super(id, dataModel,canInput,settings);
	}	
	
	public void renderHead(IHeaderResponse response) {
		response.renderCSSReference(WebResources.Streets.ComboBox.LIST_CSS);
		response.renderJavascriptReference(WebResources.Streets.ComboBox.LIST_JS);
	}
	
	protected void renderDataModel(Response response){
		response.write("<UL>");
		if (!isRequired()){
			renderItem(ListItem.empty(),response);
		}
		if (getDataModel()!=null){
			for (Iterator<ListItem> iter = getDataModel().iterator(); iter.hasNext();) {
				ListItem item = iter.next();
				if (item!=null){
					renderItem(item,response);
				}
			}
		}
		response.write("</UL>");
	}
	
	@Override
	protected void onIconClicked(Response response){
		renderDataModel(response);
	}
	
	@Override
	protected String getInitJavascript(CharSequence callbackUrl,String SettingsJS, String indicatorId){
		final String id = getMarkupId();
		String initJS = String.format("var %s=new Streets.ListComboBox('%s','%s',%s,%s);", id,id,
				callbackUrl, SettingsJS, indicatorId);
		return initJS;
	}

}
